{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Torch `unsqueeze` & `unsqueeze_`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import set_env\n",
    "import torch\n",
    "from tools.tvm_utils import verify_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.set_grad_enabled(False)\n",
    "input_shape = [10, 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Unsqueeze1(torch.nn.Module):\n",
    "    def forward(self, *args):\n",
    "        return args[0].unsqueeze(2)\n",
    "\n",
    "class Unsqueeze2(torch.nn.Module):\n",
    "    def forward(self, *args):\n",
    "        _ = args[0].unsqueeze_(2)\n",
    "        # Check whether operations after inplace unsqueeze works as expected\n",
    "        y = args[0].squeeze(2)\n",
    "        return torch.add(y, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_data = torch.rand(input_shape).float()\n",
    "verify_model(Unsqueeze1().float().eval(), input_data=input_data)\n",
    "verify_model(Unsqueeze2().float().eval(), input_data=input_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor[(5, 5), float32]\n",
      "torch.Size([5, 5, 1])\n"
     ]
    }
   ],
   "source": [
    "import tvm\n",
    "\n",
    "@torch.jit.script\n",
    "def fn(x):\n",
    "  _ = x.unsqueeze_(2)\n",
    "  y = x *2\n",
    "  return y\n",
    "m,p = tvm.relay.frontend.from_pytorch(fn, [('input', [5, 5])])\n",
    "m2 = tvm.relay.transform.InferType()(m)\n",
    "print(m2['main'].body.checked_type)\n",
    "print(fn(torch.randn(5,5)).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (326900603.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Cell \u001b[0;32mIn[6], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m    aten::index_put\u001b[0m\n\u001b[0m         ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xin",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
